// 时间格式化函数 formatDate(new Date(),"yyyy-MM-dd hh:mm:ss");
export function formatDate(date, fmt) {
  if (/(y+)/.test(fmt)) {
    fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length));
  }
  let o = {
    'M+': date.getMonth() + 1,
    'd+': date.getDate(),
    'h+': date.getHours(),
    'm+': date.getMinutes(),
    's+': date.getSeconds()
  };
  for (let k in o) {
    if (new RegExp(`(${k})`).test(fmt)) {
      let str = o[k] + '';
      fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? str : padLeftZero(str));
    }
  }
  return fmt;
};
function formatDate1(date, fmt) {
  if (/(y+)/.test(fmt)) {
    fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length));
  }
  let o = {
    'M+': date.getMonth() + 1,
    'd+': date.getDate(),
    'h+': date.getHours(),
    'm+': date.getMinutes(),
    's+': date.getSeconds()
  };
  for (let k in o) {
    if (new RegExp(`(${k})`).test(fmt)) {
      let str = o[k] + '';
      fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? str : padLeftZero(str));
    }
  }
  return fmt;
};

function padLeftZero(str) {
  return ('00' + str).substr(str.length);
}
// 传入毫秒 ,导出hh:mm:ss 的格式
export function msToTime(duration) {
  if (!duration) {
    return "";
  }
  var milliseconds = parseInt((duration % 1000) / 100)
    , seconds = parseInt((duration / 1000) % 60)
    , minutes = parseInt((duration / (1000 * 60)) % 60)
    , hours = parseInt((duration / (1000 * 60 * 60)) % 24);

  hours = (hours < 10) ? "0" + hours : hours;
  minutes = (minutes < 10) ? "0" + minutes : minutes;
  seconds = (seconds < 10) ? "0" + seconds : seconds;

  return hours + ":" + minutes + ":" + seconds;
}
var now = new Date(); //当前日期
var nowDayOfWeek = now.getDay(); //今天本周的第几天
var nowDay = now.getDate(); //当前日
var nowMonth = now.getMonth(); //当前月
var nowYear = now.getYear(); //当前年
nowYear += (nowYear < 2000) ? 1900 : 0; //
var lastMonthDate = new Date(); //上月日期
lastMonthDate.setDate(1);
lastMonthDate.setMonth(lastMonthDate.getMonth() - 1);
var lastYear = lastMonthDate.getYear();
var lastMonth = lastMonthDate.getMonth();
//格式化日期：yyyy-MM-dd
function formatDateOther(date) {
  var myyear = date.getFullYear();
  var mymonth = date.getMonth() + 1;
  var myweekday = date.getDate();
  if (mymonth < 10) {
    mymonth = "0" + mymonth;
  }
  if (myweekday < 10) {
    myweekday = "0" + myweekday;
  }
  return (myyear + "-" + mymonth + "-" + myweekday);
}
//获得某月的天数
function getMonthDays(myMonth) {
  var monthStartDate = new Date(nowYear, myMonth, 1);
  var monthEndDate = new Date(nowYear, myMonth + 1, 1);
  var days = (monthEndDate - monthStartDate) / (1000 * 60 * 60 * 24);
  return days;
}
//获得本季度的开始月份
function getQuarterStartMonth() {
  var quarterStartMonth = 0;
  if (nowMonth < 3) {
    quarterStartMonth = 0;
  }
  if (2 < nowMonth && nowMonth < 6) {
    quarterStartMonth = 3;
  }
  if (5 < nowMonth && nowMonth < 9) {
    quarterStartMonth = 6;
  }
  if (nowMonth > 8) {
    quarterStartMonth = 9;
  }
  return quarterStartMonth;
}
//获得本周的开始日期
function getWeekStartDate() {
  var weekStartDate = new Date(nowYear, nowMonth, nowDay - nowDayOfWeek);
  return formatDateOther(weekStartDate);
}
//获得本周的结束日期
function getWeekEndDate() {
  var weekEndDate = new Date(nowYear, nowMonth, nowDay + (6 - nowDayOfWeek));
  return formatDateOther(weekEndDate);
}
//获得上周的开始日期
function getLastWeekStartDate() {
  var weekStartDate = new Date(nowYear, nowMonth, nowDay - nowDayOfWeek - 7);
  return formatDateOther(weekStartDate);
}
//获得上周的结束日期
function getLastWeekEndDate() {
  var weekEndDate = new Date(nowYear, nowMonth, nowDay - nowDayOfWeek - 1);
  return formatDateOther(weekEndDate);
}
//获得本月的开始日期
function getMonthStartDate() {
  var monthStartDate = new Date(nowYear, nowMonth, 1);
  return formatDateOther(monthStartDate);
}
//获得本月的结束日期
function getMonthEndDate() {
  var monthEndDate = new Date(nowYear, nowMonth, getMonthDays(nowMonth));
  return formatDateOther(monthEndDate);
}
//获得上月开始时间
function getLastMonthStartDate() {
  var lastMonthStartDate = new Date(nowYear, lastMonth, 1);
  return formatDateOther(lastMonthStartDate);
}
//获得上月结束时间
function getLastMonthEndDate() {
  var lastMonthEndDate = new Date(nowYear, lastMonth, getMonthDays(lastMonth));
  return formatDateOther(lastMonthEndDate);
}
//获得本季度的开始日期
function getQuarterStartDate() {
  var quarterStartDate = new Date(nowYear, getQuarterStartMonth(), 1);
  return formatDateOther(quarterStartDate);
}
//或的本季度的结束日期
function getQuarterEndDate() {
  var quarterEndMonth = getQuarterStartMonth() + 2;
  var quarterStartDate = new Date(nowYear, quarterEndMonth, getMonthDays(quarterEndMonth));
  return formatDateOther(quarterStartDate);
}
export function returnTime(type) {
  let nowTime = new Date();
  var theStartTime = "",
    theEndTime = "";
  switch (type) {
    case 0:
      theStartTime = formatDate1(nowTime, "yyyy-MM-dd hh:mm:ss");
      theEndTime = formatDate1(nowTime, "yyyy-MM-dd hh:mm:ss");
      break;
    case 1: // 今天
      theStartTime = formatDate1(nowTime, "yyyy-MM-dd") + " 00:00:00";
      theEndTime = formatDate1(nowTime, "yyyy-MM-dd hh:mm:ss");
      break;
    case 2: // 过去24小时
      var timeStamp = nowTime.getTime() - 23 * 60 * 60 * 1000;
      theStartTime = formatDate1(new Date(timeStamp), "yyyy-MM-dd hh") + ":00:00";
      theEndTime = formatDate1(nowTime, "yyyy-MM-dd hh") + ":00:00";
      break;
    case 3: // 昨天
      var timeStamp = nowTime.getTime() - 24 * 60 * 60 * 1000;
      theStartTime = formatDate1(new Date(timeStamp), "yyyy-MM-dd") + " 00:00:00";
      theEndTime = formatDate1(new Date(timeStamp), "yyyy-MM-dd") + " 23:59:59";
      break;
    case 4: // 本周
      theStartTime = getWeekStartDate() + " 00:00:00";
      theEndTime = formatDate1(nowTime, "yyyy-MM-dd hh:mm:ss");
      break;
    case 5: // 上周
      theStartTime = getLastWeekStartDate() + " 00:00:00";
      theEndTime = getLastWeekEndDate() + " 23:59:59";
      break;
    case 6: // 过去七天
      var timeStamp = nowTime.getTime() - 6 * 24 * 60 * 60 * 1000;
      theStartTime = formatDate1(new Date(timeStamp), "yyyy-MM-dd hh") + ":00:00";
      theEndTime = formatDate1(nowTime, "yyyy-MM-dd hh:mm:ss");
      break;
    case 7: // 本月
      theStartTime = getMonthStartDate() + " 00:00:00";
      theEndTime = formatDate1(nowTime, "yyyy-MM-dd hh:mm:ss");
      break;
    case 8: // 上月
      theStartTime = getLastMonthStartDate() + " 00:00:00";
      theEndTime = getLastMonthEndDate() + " 23:59:59";
      break;
    case 9: // 本年
      theStartTime = formatDate1(nowTime, "yyyy") + "-01-01 00:00:00";
      theEndTime = formatDate1(nowTime, "yyyy-MM-dd hh:mm:ss");
      break;
    case 10: // 去年
      var lastYearDate = nowTime.getFullYear() - 1;
      theStartTime = lastYearDate + "-01-01 00:00:00";
      theEndTime = lastYearDate + "-12-31 23:59:59";
      break;
  }
  return {
    theStartTime: theStartTime,
    theEndTime: theEndTime
  };
}